unit unitformbusquedaempresa;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DBGrids,
  unitmainform, sqldb, db;

type

  { TFormBusquedaEmpresa }

  TFormBusquedaEmpresa = class(TForm)
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    busqueda_nombre : string;
    resultado_nombre : string;
    resultado_codigo : string;
  end;

var
  FormBusquedaEmpresa: TFormBusquedaEmpresa;

implementation

{$R *.lfm}

{ TFormBusquedaEmpresa }

procedure TFormBusquedaEmpresa.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := MainForm.PQConnection1;
  SQLQuery1.Transaction := Mainform.SQLTransaction1;
end;

procedure TFormBusquedaEmpresa.FormShow(Sender: TObject);
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from empresa where emp_nombre ilike ''%' + busqueda_nombre + '%'' order by emp_nombre';

  SQLQuery1.Open;

  DBGrid1.Columns[0].Title.Caption := 'Codigo';
  DBGrid1.Columns[1].Title.Caption := 'Nombre Empresa';

  DBGrid1.Columns[0].Width := 80;
  DBGrid1.Columns[1].Width := 190;
end;

procedure TFormBusquedaEmpresa.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;
end;

procedure TFormBusquedaEmpresa.DBGrid1CellClick(Column: TColumn);
begin
  resultado_codigo := SQLQuery1.FieldByName('emp_cdg').AsString;
  resultado_nombre := SQLQuery1.FieldByName('emp_nombre').AsString;

  Close;
end;

end.

